#include "MessageSevice.h"
#include "../CinUserMap.h"

MessageSevice::MessageSevice(/* args */)
{
}

MessageSevice::~MessageSevice()
{
}

 void MessageSevice::doHandle(const char *pDataBuffer)
 {
   NetPacket_MsgData *pData = (NetPacket_MsgData *)pDataBuffer;
   string uid = pData ->to;
   int tosd = CinUserMap::GetInstance()->findUserByUid(uid);
   int ret = 0;
   if(tosd > 0){
      ret = 1;
      NetPacket_MsgData vData;
      vData.flag = pData->flag;
      strncpy(vData.msgid, pData->msgid, sizeof(vData.msgid));
      strncpy(vData.from, pData->from, sizeof(vData.from));
      strncpy(vData.to, pData->to, sizeof(vData.to));
      strncpy(vData.sendMessage, pData->sendMessage, sizeof(vData.sendMessage));
      //转发到目标用户
      mSocket->send(tosd, NET_MSG, (const char*)&vData, sizeof(vData)); 
   }   
   
   replyMsg(ret, pData->msgid, pData->to);

   //free(pDataBuffer);
   
 }

 void MessageSevice::replyMsg(int ret, char *msgid, char *to)
 {
   NetPacket_RepMsg vData;
   vData.ret = ret;
   strncpy(vData.msgid, msgid, sizeof(vData.msgid));
   strncpy(vData.to, to, sizeof(vData.to));
   mSocket->send(peerfd, REP_MSG, (const char*)&vData, sizeof(vData)); 
 }